function [bw, idif] = ProcessImage(img, dimg0, dhimg0, cc, nstremove, nstfill, nbwarea, dthresh)

dimg = double(img);
dhimg = double(rgb2hsv(img));

[im1, im2, im3] = SubtractImagesC(dimg0, dimg, dhimg0, dhimg, cc); 

dimg(:,:,1) = im1;
dimg(:,:,2) = im2;
dimg(:,:,3) = im3;
idif = uint8(dimg);

seD = strel('diamond',nstremove);
idif = imerode(idif,seD);
I = rgb2gray(idif);
threshold = graythresh(I);
threshold = threshold*dthresh;
bw = im2bw(I,threshold);

bw = bwareaopen(bw,nbwarea);
se = strel('diamond',nstfill);
bw = imclose(bw,se);
bw = imfill(bw,'holes');
